/**
 * 
 */
package org.jms.examples.ibmmq.p2p;

import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import com.ibm.jms.JMSTextMessage;
import com.ibm.mq.jms.MQQueue;
import com.ibm.mq.jms.MQQueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.jms.MQQueueSender;
import com.ibm.mq.jms.MQQueueSession;

/**
 * @author Sudarsan
 *
 */
public class Sender {

	private MQQueueConnectionFactory connectionFactory = null;
	private MQQueueConnection connection = null;
	private MQQueueSender sender = null;
	private MQQueueSession session = null;
	private MQQueue queue = null;

	public void sendMessage() {

		try {
			connectionFactory = new MQQueueConnectionFactory();
			connectionFactory.setHostName("localhost");
			connectionFactory.setPort(1415);
			connectionFactory.setQueueManager("JMSDEMO");
			connectionFactory.setChannel("SYSTEM.DEF.SVRCONN");

			connection = (MQQueueConnection) connectionFactory
					.createConnection();

			session = (MQQueueSession) connection.createQueueSession(false,
					Session.AUTO_ACKNOWLEDGE);
			queue = (MQQueue) session.createQueue("JMSDEMO.QL");
			sender = (MQQueueSender) session.createSender(queue);
			ObjectMessage objectMessage = session.createObjectMessage();
			Product product = new Product(111,"AAA",100);
			objectMessage.setObject(product);
//
//			JMSTextMessage message = (JMSTextMessage) session
//					.createTextMessage("Hello, World !");
			connection.start();
			sender.send(objectMessage);

			System.out.println("\nMessage Sent to Queue\n");

		} catch (JMSException e) {
			e.printStackTrace();
		} finally {
			if (sender != null) {
				try {
					sender.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}

			if (session != null) {
				try {
					session.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}

			if (connection != null) {
				try {
					connection.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Sender sender = new Sender();
		sender.sendMessage();

	}

}
